/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.admin.model;

import com.common.json.JsonModel;
import com.entities.Employee;
import com.entities.EventLibrary;
import com.entities.EventRegistration;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.StoredProcedureQuery;
import org.json.simple.JSONObject;

/**
 *
 * @author Tuan Long
 */
@Stateless
public class EventLibraryFacade extends AbstractFacade<EventLibrary> implements EventLibraryFacadeLocal {

    @PersistenceContext(unitName = "BluePumpkinEJBPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public EventLibraryFacade() {
        super(EventLibrary.class);
    }

    /**
     * @EventLibraryFacade.fillEventsDataTable: return to List< -JSONObject
     * String->, with a JSONObject is the row-result of the Select statement
     * from database
     * @return List<>
     */
    @Override
    public List<String> fillEventsDataTable() {
        List<String> jsonlist = new ArrayList<String>();
        Query showAll = em.createNamedStoredProcedureQuery("EventLibrary.fillEventsDataTable");
        List<Object[]> results = showAll.getResultList();
        for (Object[] row : results) {
            int eventId = (int) row[0];
            String eventName = (String) row[1];
            String description = (String) row[2];
            String eventType = (String) row[3];
            Date timeS = (Date) row[4];
            Date timeE = (Date) row[5];
            int registered = (int) row[6];
            EventLibrary el = new EventLibrary(eventId, eventName, description, eventType, timeS, timeE);
            JSONObject jsonObj = JsonModel.eventsDataTableMessageSender(el, registered);
            String json = jsonObj.toString();
            jsonlist.add(json);
        }
        return jsonlist;
    }

    @Override
    public List<String> testUseSPParameter(String name) {
        List<String> jsonlist = new ArrayList<String>();
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("EventLibrary.testParameter");
        query.setParameter("name", name);
        List<Object[]> results = query.getResultList();
        for (Object[] row : results) {
            int eventId = (int) row[0];
            String eventName = (String) row[1];
            String description = (String) row[2];
            String eventType = (String) row[3];
            Date timeS = (Date) row[4];
            Date timeE = (Date) row[5];
            EventLibrary el = new EventLibrary(eventId, eventName, description, eventType, timeS, timeE);
            JSONObject jsonObj = JsonModel.eventMessageSender(el);
            String json = jsonObj.toString();
            jsonlist.add(json);
        }
        return jsonlist;
    }

    /**
     * @EventLibraryFacade.fillRegisteredTable: return to List< -JSONObject
     * String->, with a JSONObject is the row-result of the Select statement
     * from database
     * @return List<>
     */
    @Override
    public List<String> fillRegisteredTable() {
        List<String> jsonlist = new ArrayList<String>();
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("EventLibrary.fillRegisteredTable");
        List<Object[]> results = query.getResultList();
        for (Object[] row : results) {
            String eventName = (String) row[0];
            String username = (String) row[1];
            Employee e = new Employee(username);
            Date timeRegistered = (Date) row[2];
            EventRegistration er = new EventRegistration();
            er.setUsername(e);
            er.setTimeRegisted(timeRegistered);
            JSONObject jsonObj = JsonModel.eventRegistrationSender(er, eventName);
            String json = jsonObj.toString();
            jsonlist.add(json);
        }
        return jsonlist;
    }

    /**
     * @EventLibraryFacade.fillTotalRegistered: return to List< -JSONObject
     * String->, with a JSONObject is the row-result of the Select statement
     * from database
     * @return List<>
     */
    @Override
    public List<String> fillTotalRegistered() {        
        List<String> jsonlist = new ArrayList<String>();
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("EventLibrary.totalRegistered");
        System.out.println("Query: "+query.toString());
        List<Object[]> results = query.getResultList();
        for (Object[] row : results) {
            String username = (String) row[0];            
            int total = (int) row[1];            
            String total_str =""+total;
            JSONObject jsonObj = JsonModel.eventTotalRegistrationSender(username, total_str);
            String json = jsonObj.toString();
            jsonlist.add(json);             
        }
        return jsonlist; 
    }

}
